﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CADprogram.Base;
namespace CADprogram
{
    public partial class Form1 : Form
    {
        //字段
        ViewManager viewManager = new ViewManager();
        float Screen_x, Screen_y;
        //属性
        PointF mousepoint = new PointF();
        bool isChangeScale = true;
        public Form1()
        {
            InitializeComponent();
        }

        #region 测试功能区
        private void painImageToolStripMenuItem1_Click(object sender, EventArgs e)
        {
           this.mapView_pictureBox1.Refresh();
            Graphics graphics = this.mapView_pictureBox1.CreateGraphics(); 
            PointF[] pointFs = new PointF[] { new PointF(0, 0), new PointF(100, 200) };
            ViewManager.set_Rotate(-90);
            ViewManager.Set_pan(0,100);
            graphics.DrawLine(Pens.Red, new PointF(0, 0), new PointF(100, 200));
        }
        #endregion

        private void Pan_ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void mapView_pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            float dx = (e.Location.X - Screen_x); Screen_x = e.Location.X;
            float dy = (e.Location.Y - Screen_y); Screen_y = e.Location.Y;
            if (e.Button == MouseButtons.Middle)
            {
                this.mapView_pictureBox1.Refresh();

                CADprogram.Exception.CreateLog.log("dx:" + dx.ToString() + "  " + "dy:" + dy.ToString());
                Graphics graphics = this.mapView_pictureBox1.CreateGraphics();
                ViewManager.Set_pan(dx, dy);
                CADprogram.Exception.CreateLog.log("平移矩阵：" + ViewManager.MatrixPan.OffsetX+","+ViewManager.MatrixPan.OffsetY);
                PointF[] pointFs = new PointF[] { new PointF(0, 0), new PointF(100, 200) };
                Base.Base.Matrix_Fangse.TransformPoints(pointFs);
                ViewManager.ConversionMatrix.TransformPoints(pointFs);
                graphics.DrawLines(Pens.Red, pointFs);
            }
            mousepoint.X = Screen_x;mousepoint.Y = Screen_y;
            PointF[] p = new PointF[] { new PointF(0, this.mapView_pictureBox1.Height), mousepoint };
            ViewManager.InvConversionMatrix.TransformPoints(p);
            Base.Base.Inv_Matrixfangshe.TransformPoints(p);
            this.coo_toolStripStatusLabel1.Text ="当前鼠标位置 x:"+p[1].X.ToString() + ",y:" + p[1].Y.ToString();
            
        }

        private void commandlineToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Base.Base.Init();
            this.Scale_toolStripStatusLabel1.Text = "1:" + ViewManager.Scale.ToString();
            //事件绑定
            this.MouseWheel += Form1_MouseWheel;
        }

        private void Form1_MouseWheel(object sender, MouseEventArgs e)
        {
            //判定鼠标位置
            if (e.X >= this.mapView_pictureBox1.Location.X && e.Y >= this.mapView_pictureBox1.Location.Y)
            {
                if (isChangeScale)
                {
                    this.mapView_pictureBox1.Refresh();
                    //表示鼠标在这个范围内
                    int d = e.Delta;
                    //需要设置曲线。
                    ViewManager.Scale = ViewManager.Scale+(d / 120)>=0.001? ViewManager.Scale + (d / 120):0.001f;
                    this.Scale_toolStripStatusLabel1.Text = "1:" + ViewManager.Scale;
                    PointF[] p = new PointF[] { new PointF(0, this.mapView_pictureBox1.Height), mousepoint };
                    ViewManager.ConversionMatrix.TransformPoints(p);
                    Graphics graphics = this.mapView_pictureBox1.CreateGraphics();
                    graphics.DrawLines(Pens.Red, p);
                }
               
            }
        }

        private void 画圆ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Graphics graphics = this.mapView_pictureBox1.CreateGraphics();
            //创建绿色画刷
            SolidBrush greenBrush = new SolidBrush(Color.Green);
            //定义曲线、多边形端点坐标
            PointF point1 = new PointF(100.0f, 100.0f);
            PointF point2 = new PointF(200.0f, 50.0f);
            PointF point3 = new PointF(250.0f, 200.0f);
            PointF point4 = new PointF(50.0f, 150.0f);
            PointF point5 = new PointF(100.0f, 100.0f);
            PointF[] points = new PointF[] { point1, point2, point3, point4 };
            //填充闭合曲线
            graphics.FillClosedCurve(greenBrush, points, FillMode.Alternate, 1.0f);
        }

        private void mapView_pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Middle)
            {
                Screen_x = e.Location.X;
                Screen_y = e.Location.Y;
            }
        }
    }
}
